﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VTV.TaskManagementSystem.Entities;

namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class Employee : IConvertToLinqEntity<Employee>, ISetting<Entities.Employee>
    {
        public string ErrorMessage { get; set; }

        public Employee Convert(object baseEntity)
        {
            Employee result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.Employee)baseEntity;

                    return result = new Employee
                    {
                        EmployeeID = entity.EmployeeID,
                        FullName = String.IsNullOrEmpty(entity.FullName) ? null : entity.FullName,
                        BirthDate = entity.BirthDate,
                        Email = String.IsNullOrEmpty(entity.Email) ? null : entity.Email,
                        Phone = String.IsNullOrEmpty(entity.Phone) ? null : entity.Phone,
                        Address = String.IsNullOrEmpty(entity.Address) ? null : entity.Address,
                        IsActive = entity.IsActive,
                        Note = String.IsNullOrEmpty(entity.Note) ? null : entity.Note
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.Employee source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.Employees.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.Employee source)
        {
            try
            {
                var newSource = this.Convert(source);

                if (newSource != null)
                {
                    using (var data = new DataBaseDataContext())
                    {
                        data.Employees.Attach(newSource);
                        data.Employees.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.Employee source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var employee = data.Employees.FirstOrDefault(p => p.EmployeeID == source.EmployeeID);
                    if (employee != null)
                    {
                        employee.FullName = String.IsNullOrEmpty(source.FullName) ? null : source.FullName;
                        employee.BirthDate = source.BirthDate;
                        employee.Email = String.IsNullOrEmpty(source.Email) ? null : source.Email;
                        employee.Phone = String.IsNullOrEmpty(source.Phone) ? null : source.Phone;
                        employee.Address = String.IsNullOrEmpty(source.Address) ? null : source.Address;
                        employee.IsActive = source.IsActive;
                        employee.Note = String.IsNullOrEmpty(source.Note) ? null : source.Note;

                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public int? GetCount()
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    return data.Employees.Count();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Employee> GetAll(int start, int limit)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Employees.OrderBy(p => p.FullName).Skip(start).Take(limit).ToList();
                    var result = (from p in obj
                                  select new Entities.Employee()
                                             {
                                                 Address = String.IsNullOrEmpty(p.Address) ? string.Empty : p.Address,
                                                 BirthDate = p.BirthDate,
                                                 Email = String.IsNullOrEmpty(p.Email) ? string.Empty : p.Email,
                                                 EmployeeID = p.EmployeeID,
                                                 FullName = String.IsNullOrEmpty(p.FullName) ? string.Empty : p.FullName,
                                                 Note = String.IsNullOrEmpty(p.Note) ? string.Empty : p.Note,
                                                 Phone = String.IsNullOrEmpty(p.Phone) ? string.Empty : p.Phone,
                                                 IsActive = p.IsActive
                                             }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.Employee GetEmployee(int employeeID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = (from p in data.Employees
                                  where p.EmployeeID == employeeID
                                  select new Entities.Employee
                                             {
                                                 Address = p.Address,
                                                 BirthDate = p.BirthDate,
                                                 EmployeeID = p.EmployeeID,
                                                 FullName = p.FullName,
                                                 Note = p.Note,
                                                 IsActive = p.IsActive,
                                                 Email = p.Email,
                                                 Phone = p.Phone
                                             }).FirstOrDefault();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Employee> FilterByName(int start, int limit, string key, out int total)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Employees.Where(p => p.FullName.Contains(key) && p.IsActive).OrderBy(p => p.FullName);
                    total = obj.Count();
                    var result = (from p in obj.Skip(start).Take(limit).ToList()
                                  select new Entities.Employee()
                                  {
                                      EmployeeID = p.EmployeeID,
                                      Email = String.IsNullOrEmpty(p.Email) ? string.Empty : p.Email,
                                      FullName = String.IsNullOrEmpty(p.FullName) ? string.Empty : p.FullName
                                  }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                total = -1;
                return null;
            }
        }
    }
}
